package com.handinfo.game;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.uimenu.AbsHyperLinker;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.util.Tools;

public class ChatItem
{
	public static final byte TYPE_QUAN = -1;// 全
	public static final byte TYPE_XI = 0;// 系
	public static final byte TYPE_SI = 1;// 私
	public static final byte TYPE_TEAM = 2;// 队
//	public static final byte TYPE_ZU = 3;// 家族
	public static final byte TYPE_QU = 4;// 区
	public static final byte TYPE_WORLD = 5;// 世
	public static final byte TYPE_FAMILY = 6;// 家
	public static final byte TYPE_REALM = 7;// 阵营
	
	public static final long ID_NONE = -1;//无效
	public static final long ID_SYSTEM  = 0;
	//
	public byte m_type;
	public long m_id_sender;
	public String m_name_sender;
	public String m_content;
	public String m_name_recver;
	// Var 试图相关变量
	private int m_h;
	private int m_color;
	public AbsHyperLinker m_hypLinker;// 与字符串数组两种互斥方式
	public String[] m_contents;

	/**
	 * 创建由自己创建的私聊记录对象 发件人是自己 收件人是他人 id是收件人Id
	 * @param type
	 * @param id_sender
	 * @param name_sender
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemBySelf(byte type, String name_recver, String content, long id)
	{
		return createChatItem(type, id, GameManager.getInstance().m_role.m_name, name_recver, content);
	}

	/**
	 * 创建对世的聊天 2012-4-6 下午04:42:53 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToWorld(String content)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_WORLD, "【世】", content,ID_NONE);
	}
	
	/**
	 * 创建对世的聊天 2012-4-6 下午04:42:53 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToRealm(String content)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_REALM, "【阵】", content,ID_NONE);
	}
	
	/**
	 * 创建对家族的聊天 2012-4-6 下午04:42:29 venizeng
	 * 所有集合对自己的发言都是Id = -1
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToFamily(String content)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_FAMILY, "【家】", content,ID_NONE);
	}

	/**
	 * 创建对队伍的聊天 2012-4-6 下午04:42:41 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToTeam(String content)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_TEAM, "【队】", content, ID_NONE);
	}

	/**
	 * 创建对世的聊天 2012-4-6 下午04:42:53 venizeng
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItemToQu(String content)
	{
		return ChatItem.createChatItemBySelf(ChatItem.TYPE_QU, "【区】", content,ID_NONE);
	}

	/**
	 * 读取聊天数据
	 * @param type
	 * @param id_sender
	 * @param name_sender
	 * @param content
	 * @return
	 */
	public static ChatItem createChatItem(byte type, long id, String name_sender, String name_recver,
			String content)
	{
		ChatItem chatItem = new ChatItem();
		chatItem.m_type = type;
		chatItem.m_id_sender = id;
		chatItem.m_name_sender = name_sender;
		chatItem.m_name_recver = name_recver;
		chatItem.m_content = changeContent(type, id, name_sender, name_recver, content);
		Tools.debugPrintln("chatItem.m_content="+chatItem.m_content);
		return chatItem;
	}

	/**
	 * 读取聊天记录
	 * 
	 * @param dis
	 * @return
	 * @throws IOException
	 */
	public static final ChatItem readChatInfos(DataInputStream dis) throws IOException
	{
		byte type = dis.readByte();
		long id = dis.readLong();
		String name = dis.readUTF();
		String content = dis.readUTF();
		return createChatItem(type, id, name, GameManager.getInstance().m_role.m_name, content);
	}

	/**                     
	 * 在聊天栏中添加内容
	 * @param type
	 * @param name
	 * @param content
	 * @return
	 */
	private static final String changeContent(byte type, long id_sender, String name_sender, String name_recver,
			String content)
	{
		if (id_sender == ID_SYSTEM) {
			name_sender = "系统";
		}
		StringBuffer stringBuffer = new StringBuffer();
		switch (type) {
			case ChatItem.TYPE_SI:
//				stringBuffer.append("【私】").append(name_sender).append("对").append(name_recver).append("说：").append(content);
				stringBuffer.append("【私】").append(name_sender).append(":").append(content);
				break;
			case ChatItem.TYPE_TEAM:
//				stringBuffer.append("【队】").append(name_sender).append("对").append(name_recver).append("说：").append(content);
				stringBuffer.append("【队】").append(name_sender).append(":").append(content);
				break;
			case ChatItem.TYPE_QU:
//				stringBuffer.append("【区】").append(name_sender).append("对").append(name_recver).append("说：").append(content);
				stringBuffer.append("【区】").append(name_sender).append(":").append(content);
				break;
			case ChatItem.TYPE_WORLD:
//				stringBuffer.append("【世】").append(name_sender).append("对").append(name_recver).append("说：").append(content);
				stringBuffer.append("【世】").append(name_sender).append(":").append(content);
				break;
			case ChatItem.TYPE_XI:
//				stringBuffer.append("【系】").append(name_sender).append("对").append(name_recver).append("说：").append(content);
				stringBuffer.append("【系】").append(name_sender).append(":").append(content);
				break;
			case ChatItem.TYPE_FAMILY:
//				stringBuffer.append("【族】").append(name_sender).append("对").append(name_recver).append("说：").append(content);
				stringBuffer.append("【族】").append(name_sender).append(":").append(content);
				break;
			case ChatItem.TYPE_REALM:
				stringBuffer.append("【阵】").append(name_sender).append(":").append(content);
				break ;
		}
		return stringBuffer.toString();
	}
	

	/**
	 * 获得一条克隆聊天记录，用于主界面或者聊天界面中显示 他们具有不同的表现形式
	 * @return
	 */
	public ChatItem clone()
	{
		ChatItem item = new ChatItem();
		item.m_type = m_type;
		item.m_id_sender = m_id_sender;
		item.m_name_sender = m_name_sender;
		item.m_content = m_content;
		return item;
	}

	public int init(int w)
	{
		m_color = getDefaultColor();
		if (AbsHyperLinker.isHyperLink(m_content)) {
			m_hypLinker = AbsHyperLinker.createHyperLinker(m_content, w, 0, m_color);
			m_h = ((m_hypLinker.getRealHeight() + UITools.H_PRE - 1) / UITools.H_PRE) * UITools.H_PRE;
			m_hypLinker.setH(m_h);
			m_hypLinker.setIndex(-1) ;//去除选中框
		}
		else {
			m_contents = Tools.getStrArray(m_content, AbstractGameScreen.s_font, w);
			m_h = m_contents.length * UITools.H_PRE;
		}
		return m_h;
	}

	private int getDefaultColor()
	{
		switch (m_type) {
			case ChatItem.TYPE_SI:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_SI);
			case ChatItem.TYPE_TEAM:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_TEAM);
			case ChatItem.TYPE_QU:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_QU);
			case ChatItem.TYPE_WORLD:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_WORLD);
			case ChatItem.TYPE_XI:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_XI);
			case ChatItem.TYPE_FAMILY:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_FAMILY);
			case ChatItem.TYPE_REALM:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_QU);
			default:
				return UIManager.getPaletteColor(UIManager.COLOR_CHAT_SI);
		}
	}

	public int getH()
	{
		if (m_content == null) {
			return 0;
		}
		return m_h;
	}

	/**
	 * 在主界面进行绘制
	 */
	public void paintChatItem(Graphics g, int x, int y, int w, int h, boolean isSel)
	{
		if (m_content == null) {
			return;
		}
		int tempX = x;
		int tempY = y;
		if (m_hypLinker != null) {
			m_hypLinker.paintInRect(g, tempX, tempY);
		}
		else if (m_contents != null) {
			g.setColor(m_color);
			for (int i = 0; i < m_contents.length; i++) {
				if (m_contents[i] == null) {
					continue ;
				}
				g.drawString(m_contents[i], tempX, tempY, 0);
				tempY += UITools.H_PRE;
			}
		}
		else {
			init(w);// 重新初始化
		}
	}

}
